package windows;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class HandleListener implements ActionListener {
   JTextArea inputText,showText;
   public void setInputText(JTextArea text) {
      inputText = text;
   }
   public void setShowText(JTextArea text) {
      showText = text;
   } 
   public void actionPerformed(ActionEvent e) {
      String str=e.getActionCommand(); 
      if(str.equals("copy"))
         showText.copy();
      else if(str.equals("cut"))
         showText.cut();
      else if(str.equals("paste"))
         inputText.paste();
    else if(str.equals("open"))
    {
    	
    	File fRead =new File("E://english.txt");
    	
		try {
			Reader	in = new FileReader(fRead);
			 BufferedReader bufferRead=new BufferedReader(in);                                                                //字符流添加到缓冲流
			 String s=null;
			 
	    	  while((s=bufferRead.readLine())!=null) {
	    		
	    		  inputText.setText(s);
	    	  }
	    	  bufferRead.close();   
	    	  in.close();
	    	  	      			
		} catch (IOException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
  	      	    			
    }
    	
     else if(str.equals("save")) 
     {
    	 
    	 File fWrite = new File("E://english.txt");
   
 	try { 
 		Writer out = new FileWriter(fWrite);
         BufferedWriter bufferWrite = new BufferedWriter(out);
         bufferWrite.write(inputText.getText());
         bufferWrite.newLine();
         bufferWrite.close();
         out.close();
         JOptionPane.showMessageDialog(showText, "保存成功", "提示",JOptionPane.WARNING_MESSAGE);    
 	  }
 	  catch(IOException e1) {
         System.out.println(e1.toString());
  
}
     }
     else if(str.equals("change"))  {
    	 
    	 change();
   
     }
     
      
      }
   public void change() {
	   String news=JOptionPane.showInputDialog("替换为");
	   StringBuffer buffer=new StringBuffer(news);
	  
	inputText.setText(buffer.toString());
	buffer.replace(0,1, "");
	JOptionPane.showMessageDialog(showText, "替换过后需保存", "提示",JOptionPane.WARNING_MESSAGE);
	   
   }
   
   }
